package cn.com.xyc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xyc.dao.CostLiveMapper;
import cn.com.xyc.dao.WordBookMapper;
import cn.com.xyc.pojo.Admin;
import cn.com.xyc.pojo.CostLive;
import cn.com.xyc.pojo.CostLiveExample;
import cn.com.xyc.pojo.CostLiveExample.Criteria;
import cn.com.xyc.pojo.WordBook;
import cn.com.xyc.pojo.WordBookExample;
import cn.com.xyc.service.MyLifeService;

@Service
public class MyLifeServiceImpl implements MyLifeService {
	@Autowired
	private WordBookMapper wordBookMapper;
	@Autowired
	private CostLiveMapper costLiveMapper;


	public List<WordBook> selectWordBookListByType(String type) throws Exception {
		WordBookExample example=new WordBookExample();
		example.createCriteria().andDicTypeEqualTo(type);
		return wordBookMapper.selectByExample(example);
	}

	public boolean saveTodayCostInfo(String data, Admin user) throws Exception {
		boolean result = true;
		String[] costInfos = data.split(";");
		for (String costInfo : costInfos) {
			CostLive costLive = new CostLive();
			String[] infos = costInfo.split(",");
			costLive.setCostTime(infos[0]);
			costLive.setCostDate(new Date());
			costLive.setType(infos[1]);
			costLive.setUserId(user.getId());
			costLive.setName(infos[2]);
			costLive.setRemark(infos[3]);
			costLive.setPrice(Float.valueOf(infos[4]));
			int i = costLiveMapper.insertSelective(costLive);
			if (i <= 0) {
				result = false;
				break;
			}
		}
		return result;
	}

	public List<CostLive> selectCostLivesList(Map<String,String> params) throws Exception {
		CostLiveExample example=new CostLiveExample();
		Criteria criteria=example.createCriteria();
		if(params!=null){
			String costDataArea=params.get("costDateArea");
			String costType=params.get("costType");
			if(StringUtils.isNotBlank(costDataArea)){
				String[] area=costDataArea.split("-");
				SimpleDateFormat format=new SimpleDateFormat("MM/dd/yyyy");
				criteria.andCostDateBetween(format.parse(area[0].trim()),format.parse(area[1].trim()));
			}
			if(StringUtils.isNotBlank(costType)){
				criteria.andTypeEqualTo(costType);
			}
		}
		example.setOrderByClause("COST_DATE");
		double count=costLiveMapper.selectSumByExample(example);
		params.put("selectCount", String.valueOf(count));
		return costLiveMapper.selectByExample(example);
	}
	
	public List<CostLive> selectCostLivesList(CostLive costLive) throws Exception {
		CostLiveExample example=new CostLiveExample();
		Criteria criteria=example.createCriteria();
		if(costLive!=null){
			if(costLive.getCostDate()!=null){
				criteria.andCostDateEqualTo(costLive.getCostDate());
			}
		}
		example.setOrderByClause("COST_TIME");
		return costLiveMapper.selectByExample(example);
	}

	public Map<String, String> selectCostCountInfos() throws Exception {
		return costLiveMapper.selectCostCountInfo();
	}

	public void exportSelectCostInfos(String ids,HSSFWorkbook workbook) throws Exception {
		CostLiveExample example=new CostLiveExample();
		Criteria criteria=example.createCriteria();
		criteria.andIdIn(Arrays.asList(ids.split(",")));
		example.setOrderByClause("COST_DATE");
		List<CostLive> exportList=costLiveMapper.selectByExample(example);
        
		exportDateToExcel(workbook, exportList);
	}
	
	public void exportDateToExcel(HSSFWorkbook workbook,List<CostLive> exportList){
		HSSFSheet sheet = workbook.createSheet();
		
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		//字体加粗 居中对齐
		HSSFCellStyle style = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		// 字体大小
		font.setFontHeightInPoints((short) 11);
		// 设置字体
		font.setFontName("宋体");
		font.setCharSet(HSSFFont.DEFAULT_CHARSET);
		// 上边框
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		// 右边框
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		// 下边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		// 左边框
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		// 水平居中
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 设置自动换行
		style.setWrapText(true);
		style.setFont(font);
		
		// 产生表格标题行  
        String[] headers = new String[6];
        headers[0]="消费日期";
        headers[1]="消费时间";
        headers[2]="消费类型";
        headers[3]="消费名称";
        headers[4]="消费明细";
        headers[5]="消费金额";
        
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
        	HSSFCell cell = row.createCell(i);
        	cell.setCellStyle(style);
        	HSSFRichTextString text = new HSSFRichTextString(headers[i]);
        	cell.setCellValue(text);
        }
        int index=0;
        for(int i =0;i<exportList.size();i++){
        	CostLive costLive=exportList.get(i);
        	index++;
        	row = sheet.createRow(index);
        	for(int cellIndex=0;cellIndex<headers.length;cellIndex++){
        		HSSFCell cell=row.createCell(cellIndex);
        		cell.setCellStyle(style);
        		if(cellIndex==0){
        			cell.setCellValue(DateFormatUtils.format(costLive.getCostDate(), "yyyy年MM月dd日"));
        		}
        		if(cellIndex==1){
        			cell.setCellValue(costLive.getCostTime());
        		}
        		if(cellIndex==2){
        			cell.setCellValue(costLive.getTypeName());
        		}
        		if(cellIndex==3){
        			cell.setCellValue(costLive.getName());
        		}
        		if(cellIndex==4){
        			cell.setCellValue(costLive.getRemark());
        		}
        		if(cellIndex==5){
        			cell.setCellValue(costLive.getPrice());
        		}
        	}
        }
	}

	public List<CostLive> selectCostLivesListOfSelect2(
			Map<String, String> params) throws Exception {
		CostLiveExample example=new CostLiveExample();
		Criteria criteria=example.createCriteria();
		if(params!=null){
			Calendar cal = Calendar.getInstance();
			String costDateOfYear=params.get("costDateOfYear")==null?String.valueOf(cal.get(Calendar.YEAR)):params.get("costDateOfYear");
			String costType=params.get("costDateOfType");
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			if(StringUtils.isBlank(params.get("costDateOfMonth"))){
				String startDate=costDateOfYear+"-01-01";
				String endDate=String.valueOf(Integer.parseInt(costDateOfYear)+1)+"-01-01";
				criteria.andCostDateGreaterThanOrEqualTo(format.parse(startDate));
				criteria.andCostDateLessThan(format.parse(endDate));
			}else{
				String costDateOfMonth=params.get("costDateOfMonth");
				if(costDateOfMonth.length()==1){
					costDateOfMonth="0"+costDateOfMonth;
				}
				String startDate=costDateOfYear+"-"+costDateOfMonth+"-01";
				String endDate;
				if(Integer.parseInt(costDateOfMonth)==12){
					endDate=String.valueOf(Integer.parseInt(costDateOfYear)+1)+"-01-01";
				}else{
					endDate=costDateOfYear+"-"+String.valueOf((Integer.parseInt(costDateOfMonth)+1))+"-01";
				}
				criteria.andCostDateGreaterThanOrEqualTo(format.parse(startDate));
				criteria.andCostDateLessThan(format.parse(endDate));
			}
			
			if(StringUtils.isNotBlank(costType)){
				criteria.andTypeEqualTo(costType);
			}
		}
		example.setOrderByClause("COST_DATE");
		double count=costLiveMapper.selectSumByExample(example);
		params.put("costDateOfCount", String.valueOf(count));
		return costLiveMapper.selectByExample(example);
	}

	public void exportCostInfosByExample(HSSFWorkbook workbook,
			List<CostLive> exportList) {
		exportDateToExcel(workbook, exportList);
	}
}
